/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rie06.rmi;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;

/**
 *
 * @author clement
 */
public class WS_History {

    @Context
    private UriInfo context;
    private static String xmlhead = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><monitoring>";
    private static String xmlfoot = "</monitoring>";

    public WS_History() {
    }

    @Path("/list")
    @GET
    @Produces("application/xml")
    public String getList() {
        String xml = "";
        WS_Database bd = new WS_Database();
        bd.connect();

        ResultSet result = bd.exec("SELECT id_histo,ip_serveur,method_name  FROM RMIHISTO;");
        if (result != null) {
            try {
                while (result.next()) {

                    xml = xml + "<history>"
                            + "<id>" + result.getString("id_histo") + "</id>"
                            + "<ip>" + result.getString("ip_serveur") + "</ip>"
                            + "<method>" + result.getString("method_name") + "</method>"
                            + "</history>";
                }
            } catch (SQLException ex) {
                Logger.getLogger(WS_History.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        bd.close();
        return xmlhead + xml + xmlfoot;
    }

    @Path("/get/{id}")
    @GET
    @Produces("application/xml")
    public String getHistory(@PathParam("id") String id) {
        String xml = "";
        WS_Database bd = new WS_Database();
        bd.connect();
        ResultSet result = bd.exec("Select * FROM RMIHISTO Where id_histo='" + id + "';");
        if (result != null) {
            try {
                result.next();

                xml = xml + "<history>"
                        + "<id>" + result.getString("id_histo") + "</id>"
                        + "<ip>" + result.getString("ip_serveur") + "</ip>"
                        + "<method>" + result.getString("method_name") + "</method>"
                        + "<value>";

                String method_name = result.getString("method_name");
                if (method_name.equalsIgnoreCase("Memory")) {
                    xml = xml + "<memory>"
                            + "<free>" + result.getString("method_value") + "</free>"
                            + "</memory>";
                } else if (method_name.equalsIgnoreCase("CPU")) {
                    xml = xml + "<cpu>"
                            + "<user>" + result.getString("method_value") + "</user>"
                            + "</cpu>";
                } else if (method_name.equalsIgnoreCase("Processus")) {
                    xml = xml + "<processus>";
                    List<Proc> list_proc = null;
                    try {
                        list_proc = bd.getObject(result.getString("id_histo"));
                    } catch (Exception ex) {
                        Logger.getLogger(WS_History.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    for (Proc process : list_proc) {
                        xml = xml + "<process>"
                                + "<pid>" + process.getPid() + "</pid>"
                                + "<command>" + process.getCommand() + "</command>"
                                + "<user>" + process.getUser() + "</user>"
                                + "<cpu>" + process.getCpu() + "</cpu>"
                                + "<memory>" + process.getMem() + "</memory>"
                                + "<priority>" + process.getPriorite() + "</priority>"
                                + "</process>";
                    }

                    xml = xml + "</processus>";
                }

                xml = xml + "</value></history>";
            } catch (SQLException ex) {
                Logger.getLogger(WS_History.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        bd.close();
        return xmlhead + xml + xmlfoot;
    }

    @Path("/drop")
    @GET
    @Produces("application/xml")
    public String getDrop() {
        WS_Database bd = new WS_Database();
        bd.connect();
        bd.execUpdate("DROP TABLE IF EXISTS RMIHISTO;");
        bd.execUpdate("CREATE TABLE RMIHISTO (id_histo int AUTO_INCREMENT NOT NULL, ip_serveur VARCHAR(30), method_name VARCHAR(30), method_value LONGTEXT, PRIMARY KEY (id_histo)) ENGINE=InnoDB;");
        bd.close();
        String xml = "<message>Drop database done</message>";
        return xmlhead + xml + xmlfoot;
    }

    @GET
    @Produces("application/xml")
    public String getXml() {
        throw new UnsupportedOperationException();
    }
}
